﻿using System.Collections.Specialized;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.MefExtensions.Modularity;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Prism.Regions;
using YunBox2.Client.Infrastructure.ViewModels;

namespace YunBox2.Client.Infrastructure
{
    [ModuleExport(typeof (InfrastructureModule))]
    public class InfrastructureModule : IModule
    {
        private readonly CompositionContainer _container = ClientContext.Current.PrismContainer;

        [Import]
        private IRegionManager _regionManager;


        private void ShowHomePage()
        {
            if (ClientContext.Current.IsLogon)
            {
                _regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, "HomePage");
            }
        }

        private void ShutdownApplication()
        {
            var mainContentRegion = _regionManager.Regions[ShellRegionNames.MainContentRegion];
            if (mainContentRegion != null)
            {
                foreach (var view in mainContentRegion.Views)
                {
                    var viewModel = ((UserControl) view).DataContext as MainContentViewModel;
                    if (viewModel == null || viewModel.CanClose)
                    {
                        mainContentRegion.Remove(view);
                        continue;
                    }
                    mainContentRegion.Activate(view);
                    MessageBox.Show("尚有操作未完成，不可关闭");
                    return;
                }
            }
            Application.Current.MainWindow.Close();
        }

        private void CloseActivedMainContentView()
        {
            var mainContentRegion = _regionManager.Regions[ShellRegionNames.MainContentRegion];
            if (mainContentRegion != null)
            {
                if(mainContentRegion.ActiveViews.Any())
                {
                    mainContentRegion.Remove(mainContentRegion.ActiveViews.First());
                }
            }
        }

        #region IModule 成员

        public void Initialize()
        {
            CommonCommands.HomeCommand.RegisterCommand(new DelegateCommand(ShowHomePage));
            CommonCommands.ActicedViewClosingCommand.RegisterCommand(new DelegateCommand(CloseActivedMainContentView));
            CommonCommands.ApplicationShutdownCommand.RegisterCommand(new DelegateCommand(ShutdownApplication));
   
            _regionManager.Regions[ShellRegionNames.TopRegion].Add(_container.GetExportedValue<LogoutMenu>("LogoutMenu"), "LogoutMenu");
            _regionManager.Regions[ShellRegionNames.MainContentRegion].Add(_container.GetExportedValue<LoginPage>("LoginPage"), "LoginPage");
            _regionManager.Regions[ShellRegionNames.MainStatusBarRegion].Add(_container.GetExportedValue<StatusBarView>("StatusBarView"), "StatusBarView");
        }

        #endregion
    }
}